package cn.daomeng.firebird.core.config;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import cn.daomeng.firebird.core.handler.UserRequestHandlerInterceptor;

/**
 * Web配置
 * <br>包括拦截器配置，
 *
 * @author wf.shu
 * @version 1.0.0
 * @since 2019-03-05 16:05
 */
public class WebConfig extends WebMvcConfigurationSupport {

    /**
     * 默认Url白名单
     */
    private String[] defaultWhiteList = {"*.html",
            "*.js",
            "*.gif",
            "*.jpg",
            "*.png",
            "*.css",
            "*.ico",
            "/druid/*",
            "/swagger-ui.html",
            "/swagger-resources",
            "/swagger-resources/**",
            "/v2/api-docs/**",
            "/webjars/springfox-swagger-ui/**",
            "/assets/**"
    };

    @Value("${calorie.request.whiteList:/login}")
    private String whiteList;

    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        //需要拦截的路径
        String[] addPathPatterns = {"/**"};
        //不拦截的路径
        String[] excludePathPatterns = null;
        if (StringUtils.isNotEmpty(whiteList)) {
            excludePathPatterns = whiteList.split("\\,");
        }
        registry.addInterceptor(new UserRequestHandlerInterceptor()).addPathPatterns(addPathPatterns).excludePathPatterns(defaultWhiteList).excludePathPatterns(excludePathPatterns);
    }
}
